<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/9/8
 * Time: 13:40
 */

namespace app\admin\controller;

use app\service\ProfitService;
use app\service\StatisticalService;
use think\Db;

/**
 * 利润统计.
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月22日10:57:35
 */
class ProfitData extends Common
{
    /**
     * 构造方法.
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();

        // $this->IsLogin();
    }


    /**
     * [Index 利润列表]
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function Index()
    {


        if ('ajax' != input('get.action')) {


            return $this->fetch();
        } else {
            $params = input();

            // 条件
            $where = ProfitService::ProfitListWhere($params['param']);

            // 参数
            $data_params  = [
                'where' => $where,
                'page' => true,
                'field' => '*',
                'number' => 10,
                'table'=>'log_profits'
            ];

            $re= ProfitService::DataList($data_params);
            $re= ProfitService::DealWith($re);

            $total = ProfitService::ProfitDataTotal($where);
            //dump($total);exit;
            return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $re];
        }
    }

    /**
     * [DataDetail 支出明细]
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:57:59
     */
    public function DataDetail()
    {

        if(input("get.action") == "ajax"){

            $start=date('Y-m-d 00:00:00', strtotime(input('create_time')));
            $end=date('Y-m-d 23:59:59', strtotime(input('create_time')));

            //dump($re);exit;
             $where=[
             ['lc.create_time','>',$start],
             ['lc.create_time','<',$end],
             ];
            # 定义listRows 分页
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 10;
            }
            $info = Db::table('log_cost')->alias('lc')
                ->leftJoin(['cost_category'=>'cc'],'lc.cate_id=cc.id')->field('lc.*,cc.title cate_title')->order("create_time desc")->where($where)->paginate($listRows, false, ['query' => request()->param()]);
            $data=$info->toArray();
            foreach($data['data'] as &$v)
            {
                $v['cate_title']=empty($v['cate_title'])?$v['title']:$v['cate_title'];
                $v['price']=PriceNumberFormat($v['price'],2);
            }
            return ['code' => 0, 'msg' => '', 'count' => 2, 'data' => $data['data']];
        }else{

            return $this->fetch();

        }
    }
}